home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / RE_SEARC.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  19KB  |  707 lines

  1. /*
  2.  *        regular expression search commands for
  3.  *               MicroGnuEmacs
  4.  *
  5.  * This file contains functions to implement several of gnuemacs'
  6.  * regular expression functions for MicroGnuEmacs.  Several of
  7.  * the routines below are just minor rearrangements of the MicroGnuEmacs
  8.  * non-regular expression search functions.  Hence some of them date back
  9.  * in essential structure to the original MicroEMACS; others are modifications
  10.  * of Rich Ellison's code.  I, Peter Newton, wrote about half from scratch.
  11.  *
  12.  * Although I have nothing to do with the GNU project, these functions
  13.  * require the GNU project's regular expression package (files regex.c and
  14.  * regex.h).  Hence, this file comes under the same copyright notice
  15.  * as the GNU project's code.  As far as I know, the rest of MicroGnuEmacs
  16.  * need not since it may be used independently of any GNU project code.     In
  17.  * any case, I certainly do not warrant either the correctness or utility
  18.  * of this code.  The GNU project copyright notice follows.  Don't you
  19.  * wish they would make it a bit shorter!
  20.  */
  21.  
  22. /*
  23. GNU Emacs copying permission notice Copyright (C) 1985 Richard M. Stallman
  24.      Verbatim copies of this document, including its copyright notice,
  25.      may be distributed by anyone in any manner.
  26.      Distribution with modifications is not permitted.
  27.  
  28. GNU Emacs is distributed in the hope that it will be useful,
  29. but without any warranty.  No author or distributor
  30. accepts responsibility to anyone for the consequences of using it
  31. or for whether it serves any particular purpose or works at all,
  32. unless he says so in writing.
  33.  
  34. Everyone is granted permission to copy, modify and redistribute
  35. GNU Emacs under the following conditions:
  36.  
  37.    Permission is granted to anyone to make or distribute verbatim copies
  38.    of GNU Emacs source code as received, in any medium, provided that all
  39.    copyright notices and permission and nonwarranty notices are preserved,
  40.    and that the distributor grants the recipient permission
  41.    for further redistribution as permitted by this document,
  42.    and gives him and points out to him an exact copy of this document
  43.    to inform him of his rights.
  44.  
  45.    Permission is granted to distribute modified versions
  46.    of GNU Emacs source code, or of portions of it,
  47.    under the above conditions, provided also that all
  48.    changed files carry prominent notices stating who last changed them
  49.    and that all the GNU-Emacs-derived material, including everything
  50.    packaged together with it and not independently usable, is
  51.    distributed under the conditions stated in this document.
  52.  
  53.    Permission is granted to distribute GNU Emacs in
  54.    compiled or executable form under the same conditions applying
  55.    for source code, provided that either
  56.     A.    it is accompanied by the corresponding machine-readable
  57.       source code, or
  58.     B.    it is accompanied by a written offer, with no time limit,
  59.       to give anyone a machine-readable copy of the corresponding
  60.       source code in return for reimbursement of the cost of distribution.
  61.       This written offer must permit verbatim duplication by anyone.
  62.     C.    it is distributed by someone who received only the
  63.       executable form, and is accompanied by a copy of the
  64.       written offer of source code which he received along with it.
  65.  
  66. In other words, you are welcome to use, share and improve GNU Emacs
  67. You are forbidden to forbid anyone else to use, share and improve
  68. what you give them.   Help stamp out software-hoarding!
  69. */
  70.  
  71. #ifdef    REGEX
  72. #include    "def.h"
  73. #include    "macro.h"
  74.  
  75. #define SRCH_BEGIN    (0)            /* Search sub-codes.    */
  76. #define SRCH_FORW    (-1)
  77. #define SRCH_BACK    (-2)
  78. #define SRCH_NOPR    (-3)
  79. #define SRCH_ACCM    (-4)
  80. #define SRCH_MARK    (-5)
  81.  
  82. char    re_pat[NPAT];            /* Regex pattern        */
  83. int    re_srch_lastdir = SRCH_NOPR;     /* Last search flags. */
  84. int    casefoldsearch = TRUE;         /* Does search ignore case ? */
  85.  
  86. /* Indexed by a character, gives the upper case equivalent of the character */
  87.  
  88. static char upcase[0400] =
  89.   { 000, 001, 002, 003, 004, 005, 006, 007,
  90.     010, 011, 012, 013, 014, 015, 016, 017,
  91.     020, 021, 022, 023, 024, 025, 026, 027,
  92.     030, 031, 032, 033, 034, 035, 036, 037,
  93.     040, 041, 042, 043, 044, 045, 046, 047,
  94.     050, 051, 052, 053, 054, 055, 056, 057,
  95.     060, 061, 062, 063, 064, 065, 066, 067,
  96.     070, 071, 072, 073, 074, 075, 076, 077,
  97.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  98.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  99.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  100.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  101.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  102.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  103.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  104.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  105.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  106.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  107.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  108.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  109.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  110.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  111.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  112.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  113.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  114.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  115.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  116.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  117.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  118.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  119.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  120.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  121.   };
  122.  
  123. /*
  124.  * Search forward.
  125.  * Get a search string from the user, and search for it,
  126.  * starting at ".". If found, "." gets moved to just after the
  127.  * matched characters, and display does all the hard stuff.
  128.  * If not found, it just prints a message.
  129.  */
  130. /*ARGSUSED*/
  131. re_forwsearch(f, n) {
  132.     register int    s;
  133.  
  134.     if ((s=re_readpattern("RE Search")) != TRUE)
  135.         return (s);
  136.     if (re_forwsrch() == FALSE) {
  137.         ewprintf("Search failed: \"%s\"", re_pat);
  138.         return (FALSE);
  139.     }
  140.     re_srch_lastdir = SRCH_FORW;
  141.     return (TRUE);
  142. }
  143.  
  144. /*
  145.  * Reverse search.
  146.  * Get a search string from the     user, and search, starting at "."
  147.  * and proceeding toward the front of the buffer. If found "." is left
  148.  * pointing at the first character of the pattern [the last character that
  149.  * was matched].
  150.  */
  151. /*ARGSUSED*/
  152. re_backsearch(f, n) {
  153.     register int    s;
  154.  
  155.     if ((s=re_readpattern("RE Search backward")) != TRUE)
  156.         return (s);
  157.     if (re_backsrch() == FALSE) {
  158.         ewprintf("Search failed: \"%s\"", re_pat);
  159.         return (FALSE);
  160.     }
  161.     re_srch_lastdir = SRCH_BACK;
  162.     return (TRUE);
  163. }
  164.  
  165.  
  166.  
  167. /*
  168.  * Search again, using the same search string
  169.  * and direction as the last search command. The direction
  170.  * has been saved in "srch_lastdir", so you know which way
  171.  * to go.
  172.  */
  173. /*ARGSUSED*/
  174. /*  This code has problems-- some incompatibility(?) with
  175.     extend.c causes match to fail when it should not.
  176.  */
  177. re_searchagain(f, n) {
  178.  
  179.   if (re_srch_lastdir == SRCH_NOPR) {
  180.     ewprintf("No last search");
  181.     return (FALSE);
  182.   }
  183.  
  184.   if (re_srch_lastdir == SRCH_FORW) {
  185.     if (re_forwsrch() == FALSE) {
  186.       ewprintf("Search failed: \"%s\"", re_pat);
  187.       return (FALSE);
  188.     }
  189.     return (TRUE);
  190.   }
  191.   if (re_srch_lastdir == SRCH_BACK) {
  192.     if (re_backsrch() == FALSE) {
  193.       ewprintf("Search failed: \"%s\"", re_pat);
  194.       return (FALSE);
  195.     }
  196.     return (TRUE);
  197.   }
  198. }
  199.  
  200.  
  201. #include "regex.h"
  202. #define BYTEWIDTH 8
  203.  
  204. /* Compiled regex goes here-- changed only when new pattern read */
  205. static struct re_pattern_buffer re_buff;
  206. static char fastmap[(1 << BYTEWIDTH)];
  207.  
  208. /* regs holds boundaries of matched text */
  209. static struct re_registers regs;
  210.  
  211. /*
  212.  * Re-Query Replace.
  213.  *    Replace strings selectively.  Does a search and replace operation.
  214.  */
  215. /*ARGSUSED*/
  216. re_queryrepl(f, n) {
  217.     register int    s;
  218.     register int    rcnt = 0;    /* Replacements made so far    */
  219.     register int    plen;        /* length of found string    */
  220.     char        news[NPAT];    /* replacement string        */
  221.  
  222.     /* Casefold check */
  223.     if (!case